import { createRouter, createWebHistory } from 'vue-router';
import Login from '../views/Login.vue';
import Register from '@/views/Register.vue';
import Layout from '../layouts/Layout.vue';
import BankList from '../views/BankList.vue';
import QuestionList from '../views/QuestionList.vue';
import PaperList from '../views/PaperList.vue';
import RuleList from '../views/RuleList.vue';
import Settings from '../views/Settings.vue';


// 新增页面组件
import PaperDetail from '../views/PaperDetail.vue';

const routes = [
  { path: '/login', component: Login },
  { path: '/register', component: Register },
  {
    path: '/',
    component: Layout,
    children: [
      { path: 'banks', component: BankList },
      { path: 'questions', component: QuestionList },
      { path: 'papers', component: PaperList },
      { path: 'rules', component: RuleList },
      { path: 'settings', component: Settings },
    ]
  },
  // 动态详情页路由
  { path: '/papers/:id', component: PaperDetail },
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 路由守卫
const whiteList = ['/login', '/register'];

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  const isInWhiteList = whiteList.includes(to.path);

  if (token || isInWhiteList) {
    next();
  } else {
    next('/login');
  }
});

export default router;